//
//  XRCommonCityPickerView.m
//  XClassRoom
//
//  Created by NeiQuan on 16/11/11.
//  Copyright © 2016年 Mr-yuwei. All rights reserved.
//

#import "XRCommonCityPickerView.h"
#import "Provice.h"
@interface XRCommonCityPickerView  ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    
    NSMutableArray   *_cityPickerArray;
    UIButton         *_coverButton;
    CityPickerBlock  _Block;
    Provice    *_provice;
    City       *_city;
    Region    *_region;
}
@end
@implementation XRCommonCityPickerView
-(void)popCityPickerViewWithClickBlock:(CityPickerBlock)block
{
    _cityPickerArray=[NSMutableArray arrayWithArray:[self AA]];
    [self createPickView];
    _Block=block;
}
-(void)createPickView
{
    __weak typeof(self)weakself=self;
  
    UIButton *cover=[ UIButton buttonWithType:UIButtonTypeCustom];
    cover.frame=self.bounds;
    [cover setBackgroundColor:[ UIColor colorWithWhite:0.5 alpha:0.7]];
    [cover addTarget:weakself action:@selector(CitycoverClick) forControlEvents:UIControlEventTouchUpInside];
    _coverButton=cover;
    UIView *backView=[[ UIView alloc] initWithFrame:CGRectMake(0, kMainScreenHeight-200, kMainScreenWidth, 250)];
    [backView setBackgroundColor:[ UIColor whiteColor]];
    
 
    UIView  *toolView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 30)];
    UILabel *lable=[[UILabel alloc] initWithFrame:CGRectMake((kMainScreenWidth-70)*0.5, 5, 70, 21)];
    lable.text=@"地址选择";
    lable.font=[ UIFont systemFontOfSize:13];
    lable.textAlignment=NSTextAlignmentCenter;
    lable.textColor=[ UIColor blackColor];
    
    
    UIButton *cancleButton=[ UIButton buttonWithType:UIButtonTypeCustom];
    cancleButton.frame=CGRectMake(5, 3, 50, 25);
    [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    cancleButton.titleLabel.font=[ UIFont systemFontOfSize:13];
    [cancleButton setTitleColor:[ UIColor blackColor] forState:UIControlStateNormal];
    [cancleButton addTarget:weakself action:@selector(CitycoverClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *sureButton=[ UIButton buttonWithType:UIButtonTypeCustom];
    sureButton.frame=CGRectMake(kMainScreenWidth-60, 3, 50, 25);
    sureButton.titleLabel.font=[ UIFont systemFontOfSize:13];
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:[ UIColor blackColor] forState:UIControlStateNormal];
    [sureButton addTarget:weakself action:@selector(CitysureClick) forControlEvents:UIControlEventTouchUpInside];
    
    [toolView addSubview:lable];
    [toolView addSubview:cancleButton];
    [toolView addSubview:sureButton];
    [backView addSubview:toolView];
    
    [cover addSubview:backView];
    [self addSubview:cover];
    
    UIPickerView *picker=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 250)];
    picker.delegate=self;
    picker.dataSource=self;
    
    [backView addSubview:picker];
    [backView bringSubviewToFront:toolView];
    [self choosePlaceWithPickerView:picker];
    
}
//UIPickerViewDataSource  必须实现的协议方法
//设置列的数目
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

//设置某一列的行数目
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger selectedComponent0Row = [pickerView selectedRowInComponent:0];
    if (component == 0)
    {
        return _cityPickerArray.count;
    }
    if (component == 1)
    {
        
        Provice *provice = _cityPickerArray[selectedComponent0Row];
        return provice.citys.count;
    }
    if (component == 2)
    {
        NSInteger selectedRow = [pickerView selectedRowInComponent:1];
        Provice *provice = _cityPickerArray[selectedComponent0Row];
        City *city = provice.citys[selectedRow];
        return city.regions.count;
    }
    return 0;
    
}

//--------------------UIPickerViewDelegate 协议 可选的方法
//设置某列某行的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSInteger selectedComponent0Row = [pickerView selectedRowInComponent:0];
    
    if (component == 0) {
        
        Provice *provice = _cityPickerArray[row];
        return provice.name;
    }
    if (component == 1) {
        
        Provice *provice = _cityPickerArray[selectedComponent0Row];
        if (provice.citys.count<=row) return @" ";
        City *city = provice.citys[row];
        return city.name;
        
    }
    if (component == 2)
    {
        NSInteger selectedRow = [pickerView selectedRowInComponent:1];
        Provice *provice = _cityPickerArray[selectedComponent0Row];
        City *city = provice.citys[selectedRow];
        if (city.regions.count<=row) return @" ";
        Region *region = city.regions[row];
        return region.name;
    }    return 0;

}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 21;
}
//当选中某一行的时候，pickerView 重新加载
//选中某列的某一行会调用的方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    //刷新
    [pickerView  reloadAllComponents];
    //修改选中行 为 0行
    [pickerView selectRow:row inComponent:component animated:YES];
    
    [self choosePlaceWithPickerView:pickerView];
    
}
//自定义picker
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *pickerLabel = (UILabel*)view;
    if (!pickerLabel) {
        pickerLabel = [[UILabel alloc]init];
        pickerLabel.font = [UIFont systemFontOfSize:14];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor = [UIColor lightGrayColor];
    ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor = [UIColor lightGrayColor];
    
    return pickerLabel;
    
}
- (void)choosePlaceWithPickerView:(UIPickerView *)pickerView
{
    NSInteger selectedComponent0Row = [pickerView selectedRowInComponent:0];
    Provice *provice = _cityPickerArray[selectedComponent0Row];
    _provice=provice;
    
    NSInteger selectedComponent1Row = [pickerView selectedRowInComponent:1];
    City *city = provice.citys[selectedComponent1Row];
    _city=city;
    
    NSInteger selectedComponent2Row = [pickerView selectedRowInComponent:2];
    Region *reigon = city.regions[selectedComponent2Row];
    _region=reigon;
    //在这里需要记录一下
}

-(NSArray *)AA
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"all" ofType:@"json"];
    NSString *s = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    
    NSData *data = [s dataUsingEncoding:NSUTF8StringEncoding];
    
    NSArray *dicArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *array = [Provice mj_objectArrayWithKeyValuesArray:dicArray];
    
    return array;
    
}
-(void)CitycoverClick
{
    [_coverButton removeFromSuperview];
    [self removeFromSuperview];
}
#pragma mark --确定事件
-(void)CitysureClick
{
    [_coverButton removeFromSuperview];
    [self removeFromSuperview];
    if (_Block) {
        _Block(_provice,_city,_region);
    }
}


@end
